package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //设置一些默认值
        dish.setStatus(StatusConstant.ENABLE);

        //把菜品添加到数据中
        dishMapper.insert(dish);
        log.info("=====菜品id={}",dish.getId());

        //获取口味的集合
        if (!dishDTO.getFlavors().isEmpty()) {
            List<DishFlavor> flavors = dishDTO.getFlavors();
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            //把菜品对应的口味添加到口味表中
            dishFlavorMapper.insert(flavors);
        }


    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        //1.开启分页
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        //2.查询分页数据
        Page<DishVO> pageList = dishMapper.pageQuery(dishPageQueryDTO);

        //3.把查询到的结果封装为PageResult
        PageResult pageResult = new PageResult();
        pageResult.setTotal(pageList.getTotal());
        pageResult.setRecords(pageList.getResult());

        return pageResult;
    }

    @Override
    public void setStatus(Integer status, Long id) {
        //修改dish表中指定id的status值
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);

        dishMapper.update(dish);
    }

    @Override
    public void update(DishDTO dishDTO) {
        //1.先创建Dish对象
        Dish dish = new Dish();
        //2.把DishDto中的数据复制到Dish对象中
        BeanUtils.copyProperties(dishDTO,dish);
        //3.修改dish表中的数据
        dishMapper.update(dish);

        //4.删除口味表中dish_id对应的口味数据
        dishFlavorMapper.delete(dish.getId());

       // 4. 修改菜品口味的数据
        if (!dishDTO.getFlavors().isEmpty()) {
            List<DishFlavor> flavors = dishDTO.getFlavors();
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            dishFlavorMapper.insert(flavors);
        }



    }

    @Override
    public DishVO getDishById(Long id) {
        //根据id获取菜品
        Dish  dish = dishMapper.getDishById(id);
        //创建DishVO 对象
        DishVO dishVO = new DishVO();
        //根据菜品id查询菜品的口味
        List<DishFlavor> dishFlavors = dishFlavorMapper.getDishFlavorByDishId(id);
        dishVO.setFlavors(dishFlavors);
        BeanUtils.copyProperties(dish,dishVO);

        return dishVO;
    }

    @Override
    public void delete(Long[] ids) {
        //删除指定id的菜品
        dishMapper.delete(ids);

        //删除指定菜品id的口味
        dishFlavorMapper.deleteByDishId(ids);
    }

    @Override
    public List<Dish> getDishByCategory(Long categoryId) {
        List<Dish> dishList = dishMapper.getDishByCategoryId(categoryId);
        return dishList;
    }

    @Override
    public List<DishVO> getDishVOByCategoryId(Long categoryId) {

        //先获取分类id对应的菜品
        List<Dish> dishList = dishMapper.getDishByCategoryId(categoryId);

        //再将菜品转换为DishVO
        List<DishVO> dishVOList = dishList.stream().map(dish -> {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish, dishVO);

            List<DishFlavor> flavors = dishFlavorMapper.getDishFlavorByDishId(dish.getId());
            dishVO.setFlavors(flavors);
            return dishVO;
        }).collect(Collectors.toList());

        return dishVOList;
    }
}
